package com.tg.app.activity.device.add;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.appbase.custom.BuildConfig;
import com.tange.module.camera.hub.CameraHub;
import com.tg.app.R;
import com.base.BaseActivity;
import com.tg.app.helper.ActivityHelper;
import com.tg.app.report.DeviceAddReport;
import com.tg.app.report.DeviceAddReportBean;


public class AddWifiDeviceActivity extends BaseActivity implements  View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_add_wifi_device);
        hideActionBar();
        initView();
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override
    protected void initView() {
        findViewById(R.id.ap_add_rel).setOnClickListener(this);
        findViewById(R.id.qrcode_add_rel).setOnClickListener(this);
        findViewById(R.id.rl_choose_lan).setOnClickListener(this);
        findViewById(R.id.back_toolbar).setOnClickListener(this);

        View spLine = findViewById(R.id.qrcode_wired_sp_line);

        findViewById(R.id.ap_add_rel).setVisibility(BuildConfig.device_wifiCamera_addByFast ? View.VISIBLE : View.GONE);
        findViewById(R.id.qrcode_add_rel).setVisibility(BuildConfig.device_wifiCamera_addByQrCode ? View.VISIBLE : View.GONE);
        findViewById(R.id.rl_choose_lan).setVisibility(BuildConfig.device_wifiCamera_addByWired ? View.VISIBLE : View.GONE);

        if (!BuildConfig.device_wifiCamera_addByQrCode || !BuildConfig.device_wifiCamera_addByWired) {
            spLine.setVisibility(View.GONE);
        } else {
            spLine.setVisibility(View.VISIBLE);
        }

        // 只匹配了AP类型，那么直接去AP
        if (BuildConfig.device_wifiCamera_addByFast && !BuildConfig.device_wifiCamera_addByQrCode && !BuildConfig.device_wifiCamera_addByWired) {
            launchAp();
            finish();
            return;
        }

        // 只匹配了扫码类型，那么直接去扫码
        if (!BuildConfig.device_wifiCamera_addByFast && BuildConfig.device_wifiCamera_addByQrCode && !BuildConfig.device_wifiCamera_addByWired) {
            launchQrCode();
            finish();
            return;
        }

        // 只匹配了有线类型，那么直接去有线
        if (!BuildConfig.device_wifiCamera_addByFast && !BuildConfig.device_wifiCamera_addByQrCode && BuildConfig.device_wifiCamera_addByWired) {
            launchLan();
            finish();
            return;
        }

    }
    @Override

    public void onClick(View v) {

        int id = v.getId();
        if (id == R.id.ap_add_rel) {
            launchAp();
        } else if (id == R.id.qrcode_add_rel) {
            launchQrCode();
        } else if (id == R.id.rl_choose_lan) {
            launchLan();
        } else if (id == R.id.back_toolbar) {
            this.finish();
        }

    }

    private void launchLan() {
        DeviceAddReportBean bean = DeviceAddReport.getInstance().getReportBean();
        ActivityHelper.goToCodeWifiDeviceActivity(this, AddFragmentTabActivity.DEVICE_ADD_TYPE_LAN);
        if (bean != null) {
            bean.title = "wifi_lan";
        }
        if (bean!=null){
            DeviceAddReport.getInstance().setReportBean(bean);
        }
    }

    private void launchQrCode() {
        DeviceAddReportBean bean = DeviceAddReport.getInstance().getReportBean();
        if (bean != null) {
            bean.title = "wifi_device_scan";
        }
        ActivityHelper.goToCodeWifiDeviceActivity(this, AddFragmentTabActivity.DEVICE_ADD_TYPE_QRCODE);
        if (bean!=null){
            DeviceAddReport.getInstance().setReportBean(bean);
        }
    }

    private void launchAp() {
        DeviceAddReportBean bean = DeviceAddReport.getInstance().getReportBean();
        if (!CameraHub.getInstance().isEnableLocalApConnect()) {
            if (bean != null) {
                bean.title = "wifi_ap";
            }
        }

        ActivityHelper.goToWifiDeviceByHotspot(this);
        if (bean!=null){
            DeviceAddReport.getInstance().setReportBean(bean);
        }
    }
}
